<template>
	<view class="page">
		<Header :title="'全部订单'"></Header>
		<view class="wrap" :style="{height : `calc(100vh - ${navBarHeight})`}">
			<u-tabs class="tabs" lineHeight="6rpx" :list="list" @change="changeTabs" :scrollable="true"
				lineColor="#00A0E9" :current="current" :activeStyle="{
					    color: '#00A0E9'}" :inactiveStyle="{
					    color: '#666666'}"></u-tabs>

			<swiper class="swiper-box" :current="swiperCurrent" @change="currentChange">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%; width: 100%" @scrolltolower="ReachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in orderList" :key="res.id">
								<view class="top">
									<view class="left">
										<view class="store">{{ res.createTime }}</view>
									</view>
									<view class="right1" v-if="res.payStatus==0">
										待付款
									</view>
									<view class="right1" v-else-if="res.payStatus==1">
										<view class="" v-if="res.refundStatus==6">待退款</view>
										<view class="" v-else-if="res.refundStatus==7">已退款</view>
										<view class="" v-else-if="res.refundStatus==8">退款失败</view>
										<view class="" v-else>已支付</view>
									</view>
									<view class="right1" v-else-if="res.payStatus==2">
										<view class="" v-if="res.refundStatus==6">待退款</view>
										<view class="" v-else-if="res.refundStatus==7">已退款</view>
										<view class="" v-else-if="res.refundStatus==8">退款失败</view>
										<view class="" v-else>已取消</view>
									</view>

									<view class="right1" v-else-if="res.payStatus==3">
										<view class="" v-if="res.refundStatus==6">待退款</view>
										<view class="" v-else-if="res.refundStatus==7">已退款</view>
										<view class="" v-else-if="res.refundStatus==8">退款失败</view>
										<view class="" v-else>待发货</view>
									</view>
									<view class="right1" v-else-if="res.payStatus==4">
										<view class="" v-if="res.refundStatus==6">待退款</view>
										<view class="" v-else-if="res.refundStatus==7">已退款</view>
										<view class="" v-else-if="res.refundStatus==8">退款失败</view>
										<view class="" v-else>待收货</view>
									</view>
									<view class="right1" v-else-if="res.payStatus==5">
										<view class="" v-if="res.refundStatus==6">待退款</view>
										<view class="" v-else-if="res.refundStatus==7">已退款</view>
										<view class="" v-else-if="res.refundStatus==8">退款失败</view>
										<view class="" v-else>已完成</view>
									</view>
									<view class="right1" v-else-if="res.refundStatus==6">
										待退款
									</view>
									<view class="right1" v-else-if="res.refundStatus==7">
										已退款
									</view>
									<view class="right1" v-else-if="res.refundStatus==8">
										退款失败
									</view>
								</view>
								<view class="item" :key="index" @click="goDetail(res)">
									<view class="left">
										<view class="dianpu">
											<!-- {{ res.store }} -->
										</view>
										<image :src="res.productIconUrl" mode="aspectFill"
											style="width: 200rpx;height: 200rpx;border-radius: 10rpx;"></image>
									</view>
									<view class="content">
										<view class=""
											style="display: flex;width: 400rpx;justify-content: space-between;">
											<view style="font-size: 28rpx;">{{ res.body }}</view>
											<view class="price"><span style="font-size: 32rpx;color: red;">￥</span>
												<span
													style="font-size: 40rpx;font-weight: 600;color: red;">{{ res.needPayAmount }}</span>
											</view>
										</view>
										<view class="type">数量：{{ res.buyCount }}</view>
									</view>
								</view>
								<!-- 按钮 -->
								<view class="BtnBox">
									<!-- 待付款 -->
									<view class="plainBtn" v-if="res.payStatus==0" @click="open(res,'取消订单')">取消订单
									</view>
									<view class="orangeBtn" v-if="res.payStatus==0" @click="goDetail(res)">去支付</view>
									<!-- 已支付 -->
									<view class="" v-if="res.refundStatus!=6">
										<view class="plainBtn" v-if="res.payStatus==5||res.deliverStatus==3"
											@click="goRefund(res)">申请退款</view>
									</view>

									<!-- 待发货 -->
									<!-- <view class="plainBtn" v-if="res.payStatus==3" @click="goLogisticsDetails(res)">查看物流
									</view> -->
									<view class="orangeBtn" v-if="res.deliverStatus==3 & res.refundStatus!=6"
										@click="remindShipments(res.id)">提醒发货
									</view>
									<!-- 待收货 -->
									<view class="plainBtn" v-if="res.deliverStatus==4" @click="goLogisticsDetails(res)">
										查看物流
									</view>
									<view class="orangeBtn" v-if="res.payStatus==4" @click="open(res,'确认收货')">确认收货
									</view>
									<!-- 	
									<view class="orangeBtn" v-if="res.payStatus==6 || res.payStatus==7 && auditStatus==2"
										@click="goRefund(res,'申请修改')">申请修改</view> -->
									<!-- <view class="orangeBtn" v-if="res.payStatus==6 || res.payStatus==7"
										@click="goRefundDetails(res)">退款详情</view> -->
								</view>
							</view>
							<view class="emptyBox" v-if="orderList.length==0 && isMouted">
								<image class="emptyIcon" src="../../../static/empty.png" mode="aspectFill"></image>
								<view class="emptyText">
									暂无相关订单！
								</view>
							</view>
							<view class="loadmoreBox" v-if="orderList.length!=0">
								<u-loadmore :status="loadStatus" :icon-type="iconType" />
							</view>
						</view>

					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 取消订单模态框 -->
		<u-modal :show="show" :content='content'>
			<template slot="confirmButton">
				<view class="zdyConfirmButton">
					<view class="noBtn" @click="show=false">我在想想</view>
					<view class="yesBtn" @click="cancelOrder(res)">取消订单</view>
				</view>
			</template>
		</u-modal>
		<!-- 确认收货模态框 -->
		<u-modal :show="show1" :content='content1'>
			<template slot="confirmButton">
				<view class="zdyConfirmButton">
					<view class="noBtn" @click="show1=false">取消</view>
					<view class="yesBtn" @click="confirmReceipt(res)">确认收货</view>
				</view>
			</template>
		</u-modal>
		<!-- 申请退款弹出层 -->
		<!-- <u-popup :show="showPopup" :closeable="true" @close="closePopup" @open="openPopup" :round="10" mode="bottom">
			<view class="PopupBox">
				<view class="title">
					选择需要退货的商品
				</view>
				<view class="PopupContent" v-for="(orderItem,key) in refundList" :key="orderItem.oid">
					<view class="selectBox" @click="clickSelect(refundList[key])">
						<view v-if="orderItem.selected" class="iconfont icon-icon_right"></view>
					</view>
					<view class="main">
						<image class="pic" src="@/static/card1.png" mode="widthFix"></image>
						<view class="mainRight">

							<view class="shopName">{{orderItem.name}}</view>
							<view class="priceBox">

								<view class="price">
									<span class="priceIcon">￥</span>
									{{orderItem.price}}
								</view>
								<view class="number">
									x{{orderItem.number}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="submitBox">
					<view class="selectBox" @click="allSelect">
						<view v-if="AllActive" class="iconfont icon-icon_right"></view>
					</view>
					<span @click="allSelect" style="margin-right: 15rpx;color:#333333;font-size:28rpx">全选</span>

					<view style="color:#333333;font-size: 28rpx">
						已选<span style="color:#FF9A19">1</span>件，退款：
						<span style="color:#D72A2A;font-size:24rpx">￥</span>
						<span style="color:#D72A2A;font-size:38rpx;font-weight: bold;">{{sum}}</span>
					</view>
					<view class="submit" @click="submitRefund">申请退款</view>
				</view>
			</view>
		</u-popup> -->



	</view>
</template>

<script>
	import Header from '@/components/header/header.vue'
	export default {
		data() {
			return {
				background: {
					backgroundColor: '#21B070'
				},

				show: false,
				content: '您还未支付,是否取消订单',

				show1: false,
				content1: '如你已收到商品,请确认收货',


				orderList: [],
				list: [{
						name: '全部'
					},
					{
						name: '待付款'
					},
					{
						name: '待发货'
					},
					{
						name: '待收货'
					},
					{
						name: '已完成'
					},
					{
						name: '待退款'
					},
					{
						name: '已退款'
					}, {
						name: '退款失败'
					},
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				status: -1,

				pageNum: 1,
				pageSize: 8,
				loadStatus: 'loadmore',
				iconType: 'flower',
				total: 0,
				isMouted: false,

				id: '',

				// 申请退款
				showPopup: false,
				refundList: [{
					oid: 1,
					price: 100,
					number: 1,
					name: '测试商品'
				}, {
					oid: 2,
					price: 100,
					number: 1,
					name: '测试商品'
				}],
				AllActive: false,
				sum: 0,

				navBarHeight: 0, //状态栏高度 + 导航栏44px



			};
		},
		components: {
			Header
		},
		created() {
			this.setNavBarHeight()


		},
		onLoad(e) {

			if (e.status == undefined) {
				this.status = -1
			} else {
				this.status = e.status
			}

			if (this.status == -1) {
				this.current = 0
			} else if (this.status == 0) {
				this.current = 1
			} else if (this.status == 3) {
				this.current = 2
			} else if (this.status == 4) {
				this.current = 3
			} else if (this.status == 5) {
				this.current = 4
			} else if (this.status == 6) {
				this.current = 5
			} else if (this.status == 7) {
				this.current = 6
			} else if (this.status == 8) {
				this.current = 7
			}
			this.getList();
		},
		computed: {},
		methods: {
			async setNavBarHeight() {
				let that = this
				console.log(1)
				wx.getStorage({
					key: 'navBarHeight',

					success(res) {
						// 换算为rpx单位 
						that.navBarHeight = res.data * 2 + 'rpx'
					}
				})

			},

			// 查看退款详情
			goRefundDetails(res) {
				uni.navigateTo({
					url: '/pages/my/order/refundDetails?id=' + res.id
				})
			},
			goRefund(res) {
				// if (type == '申请修改') {
				// 	uni.navigateTo({
				// 		url: '/pages/my/order/applicationDrawback?item=' + JSON.stringify(res) + '&type=申请修改'
				// 	})
				// } else {
				// 	uni.navigateTo({
				// 		url: '/pages/my/order/applicationDrawback?item=' + JSON.stringify(res)
				// 	})
				// }
				uni.navigateTo({
					url: '/pages/my/order/orderDetail?id=' + res.id
				});
			},
			remindShipments(id) {
				let that = this
				this.$utils.throttle(function() {
					that.$api.request('pay/order/remind', 'get', {
						id: id
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '提醒成功',
								icon: 'success'
							})
						}
					})
				}, 1100)()

			},
			openPopup() {

			},
			closePopup() {
				this.showPopup = false
			},
			// 选择退款商品
			clickSelect() {

			},
			//全选退款商品
			allSelect() {

			},
			// 申请退款
			// submitRefund(){

			// },

			open(val, type) {
				switch (type) {
					case '取消订单':
						this.show = true;
						this.id = val.id
						break;
					case '确认收货':
						this.show1 = true;
						this.id = val.id
						break;
				}


			},
			// 取消订单事件
			cancelOrder(val) {
				this.show = false
				let params = {
					id: this.id,
				}
				this.$api.request('pay/order/cancel', 'get', params).then(res => {
					if (res.code == 200) {
						this.orderList = []
						this.getList()
						uni.showToast({
							title: '取消成功',
							icon: 'success'
						})


					} else {
						uni.showToast({
							title: '取消失败',
							icon: 'error'
						})
					}
				})
			},
			// 确认收货事件
			confirmReceipt() {
				this.show1 = false
				let params = {
					id: this.id,
				}
				this.$api.request('admin/order/client/complete', 'get', params).then(res => {
					if (res.code == 200) {
						this.orderList = []
						this.getList()
						uni.showToast({
							title: '确认成功',
							icon: 'success'
						})


					} else {
						uni.showToast({
							title: '确认失败',
							icon: 'error'
						})
					}
				})
			},


			// 查看物流
			goLogisticsDetails(val) {
				uni.navigateTo({
					url: `/pages/my/order/logisticsDetails?id=${val.id}&userAddress=${val.userAddress}`
				});
			},
			currentChange(e) {
				// // 用户触摸滑动切换时,更新data中的current
				// console.log(e.detail.current)
				// this.current = e.detail.current
				// this.orderList = []
				// this.pageNum = 1,
				// this.getList()
			},
			ReachBottom() {
				console.log('触发了')
				console.log(this.orderList.length)
				if (this.orderList.length == this.total || this.orderList.length > this.total) {
					console.log('没有更多')
					this.loadStatus = "nomore" //数据数量等于大于 总数时 阻止后续代码 不再请求
					return
				}
				//触底多次 只触发一次请求
				if (this.loadStatus == 'loading') {
					return
				}
				this.loadStatus = 'loading'; //设置触底加载组件状态 为正在加载
				this.pageNum = ++this.pageNum; //请求下一页

				this.getList(); //请求的接口数据
				if (this.orderList.length == this.total) {
					this.loadStatus = 'nomore'
				} else {
					this.loadStatus = 'loading'
				};
			},
			changeTabs(item) {
				console.log(111111111);
				if (item.index == 0) {
					this.status = -1
				} else if (item.index == 1) {
					this.status = 0
				} else if (item.index == 2) {
					this.status = 3
				} else if (item.index == 3) {
					this.status = 4
				} else if (item.index == 4) {
					this.status = 5
				} else if (item.index == 5) {
					this.status = 6
				} else if (item.index == 6) {
					this.status = 7
				} else if (item.index == 7) {
					this.status = 8
				}
				this.orderList = []
				this.pageNum = 1
				this.isMouted = false
				if (this.status == -1) {
					this.getList();
				} else if (this.status == 0 || this.status == 5) {
					this.getList();
				} else if (this.status == 3 || this.status == 4) {
					this.getList2();
				} else {
					this.getList1();
				}
			},

			// 跳转详情
			goDetail(val) {
				uni.navigateTo({
					url: '/pages/my/order/orderDetail?id=' + val.id
				});
			},

			// 页面数据
			async getList2() {
				let params = {
					deliverStatus: this.status,
					pageSize: this.pageSize,
					pageNum: this.pageNum
				}
				let res = await this.$api.request('pay/order/list', 'get', params)
				this.orderList.push(...res.data.list)
				this.total = res.total
				this.isMouted = true
				if (this.orderList.length == this.total || this.orderList.length > this.total) {
					this.loadStatus = "nomore" //数据数量等于大于 总数时 阻止后续代码 不再请求
					return
				}
			},

			// 页面数据
			async getList1() {
				let params = {
					refundStatus: this.status,
					pageSize: this.pageSize,
					pageNum: this.pageNum
				}
				let res = await this.$api.request('pay/order/list', 'get', params)
				this.orderList.push(...res.data.list)
				this.total = res.total
				this.isMouted = true
				if (this.orderList.length == this.total || this.orderList.length > this.total) {
					this.loadStatus = "nomore" //数据数量等于大于 总数时 阻止后续代码 不再请求
					return
				}
			},


			// 页面数据
			async getList() {
				let params = {
					payStatus: this.status,
					pageSize: this.pageSize,
					pageNum: this.pageNum
				}
				let res = await this.$api.request('pay/order/list', 'get', params)
				this.orderList.push(...res.data.list)
				this.total = res.total
				this.isMouted = true
				if (this.orderList.length == this.total || this.orderList.length > this.total) {
					this.loadStatus = "nomore" //数据数量等于大于 总数时 阻止后续代码 不再请求
					return
				}
			},
		}
	};
</script>

<style lang="scss">
	/* #ifndef H5 */
	page {
		height: 100%;
		/* background-color: #f2f2f2; */
	}

	/* #endif */
	.zdyConfirmButton {
		display: flex;
		justify-content: center;
		padding: 0 20rpx;

		.yesBtn {
			height: 60rpx;
			width: 170rpx;
			border-radius: 100rpx;
			text-align: center;
			line-height: 60rpx;
			color: #fff;
			font-size: 24rpx;
			border: 1rpx solid #dee2e6;
			margin-left: 20rpx;
			background-color: #dc3545;
		}

		.noBtn {
			height: 60rpx;
			width: 170rpx;
			border-radius: 100rpx;
			text-align: center;
			line-height: 60rpx;
			color: #000;
			font-size: 24rpx;
			border: 1rpx solid #dee2e6;
		}
	}
</style>

<style lang="scss" scoped>
	.page {
		background-color: #f5f5f5;
	}

	.tabs {
		width: 750rpx;
		height: 88rpx;
		background-color: #fff;
		z-index: 600;
	}



	.emptyBox {
		width: 100%;
		height: 50vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.emptyText {
			font-size: 32rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.emptyIcon {
			width: 200rpx;
			height: 200rpx;


		}
	}

	.order {
		background-color: #ffffff;
		margin: 20rpx 30rpx;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 30rpx;
		font-size: 28rpx;

		.BtnBox {
			margin-top: 20rpx;
			display: flex;
			justify-content: flex-end;

			.plainBtn {
				border-radius: 100rpx;
				height: 50rpx;
				width: 140rpx;
				border: 1rpx solid #dee2e6;
				color: #212529;
				font-size: 24rpx;
				line-height: 50rpx;
				text-align: center;
				cursor: pointer;
				transition: 0.2s;
				margin-left: 20rpx;

				&:hover {
					opacity: 0.8;
				}
			}

			.orangeBtn {
				margin-left: 20rpx;
				border-radius: 100rpx;
				height: 50rpx;
				width: 140rpx;
				border: 1rpx solid #19CA7B;
				font-size: 24rpx;
				color: #19CA7B;
				line-height: 50rpx;
				text-align: center;
				cursor: pointer;
				transition: 0.2s;

				&:hover {
					opacity: 0.8;
				}
			}
		}

		.top {
			display: flex;
			justify-content: space-between;
			padding-bottom: 20rpx;
			border-bottom: 1px solid #ececec;

			.left {
				display: flex;
				align-items: center;

				.store {
					font-size: 24rpx;
					color: #999999;
				}
			}

			.right {
				color: #26acf5;
			}

			.right1 {
				color: #f55726;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 30rpx;

				image {
					width: 130rpx;
					height: 130rpx;
					// border-radius: 10rpx;
				}

				.dianpu {
					font-size: 26rpx;
					line-height: 36rpx;
					color: #333333;
					margin-bottom: 16rpx;
				}
			}

			.content {
				margin-top: 56rpx;

				.title {
					font-size: 28rpx;
					line-height: 36rpx;
					color: #333333;
				}

				.type {
					margin: 20rpx 0 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 42rpx;
				margin-top: 52rpx;
				text-align: right;
				color: #333333;
				font-size: 24rpx;
				line-height: 30rpx;
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;

			justify-content: flex-end;
			align-items: center;

			.btn {
				line-height: 58rpx;
				width: 140rpx;
				border-radius: 28rpx;
				border: 1rpx solid #999999;
				font-size: 24rpx;
				text-align: center;
				color: #333333;
				margin-left: 20rpx;
			}

			.evaluate {
				color: #00aaff;
				border: 1rpx solid #00aaff;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;

		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
		box-sizing: border-box;
	}

	.PopupBox {
		position: relative;
		padding-bottom: 118rpx;

		.title {
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #000000;
			padding: 36rpx 20rpx 72rpx;
		}

		.submitBox {
			display: flex;
			align-items: center;
			padding: 0 20rpx;
			box-sizing: border-box;
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 98rpx;
			background: #FFFFFF;
			box-shadow: 0 2rpx 6rpx #000;

			.selectBox {
				position: relative;
				width: 30rpx;
				height: 30rpx;
				background: #FFFFFF;
				border: 2px solid #DCDCDC;
				border-radius: 50%;
				box-sizing: border-box;
				margin-right: 15rpx;





				.icon-icon_right {
					position: absolute;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
					display: flex;
					justify-content: center;
					align-items: center;
					width: 30rpx;
					height: 30rpx;
					font-size: 19rpx;
					color: #fff;
					background-color: #FF9A19;
					border-radius: 50%;
				}
			}

			.submit {
				margin-left: auto;
				height: 60rpx;
				width: 160rpx;
				background: #D72A2A;
				border-radius: 100rpx;
				font-size: 28rpx;
				color: #fff;
				text-align: center;
				line-height: 60rpx;
			}
		}

		.PopupContent {
			display: flex;
			align-items: center;
			padding: 0 20rpx;

			.selectBox {
				position: relative;
				width: 30rpx;
				height: 30rpx;
				background: #FFFFFF;
				border: 2px solid #DCDCDC;
				border-radius: 50%;
				box-sizing: border-box;
				margin-right: 22rpx;


				.icon-icon_right {
					position: absolute;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
					display: flex;
					justify-content: center;
					align-items: center;
					width: 30rpx;
					height: 30rpx;
					font-size: 19rpx;
					color: #fff;
					background-color: #FF9A19;
					border-radius: 50%;
				}
			}

			.main {
				display: flex;
				flex: 1;

				.pic {
					width: 187rpx;
					height: 187rpx;
					background: #F5F5F5;
					border-radius: 10rpx;
					margin-right: 38rpx;
				}

				.mainRight {
					flex: 1;
					padding: 18rpx 0 9rpx 0;
					display: flex;
					flex-direction: column;
					justify-content: space-between;


					.shopName {
						font-size: 30rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #333333;
						line-height: 34rpx;
					}

					.priceBox {
						width: 100%;
						display: flex;
						justify-content: space-between;

						.number {
							color: #333333;
							font-size: 24rpx;
							height: 100%;
							display: flex;
							flex-direction: column;
							justify-content: flex-end;
							padding-bottom: 5rpx;
							box-sizing: border-box;
						}

						.priceIcon {
							font-size: 24rpx;
							font-family: PingFang SC;
							font-weight: 400;
							color: #D72A2A;
						}

						.price {
							font-size: 32rpx;
							font-family: PingFang SC;
							font-weight: 400;
							color: #D72A2A;
						}
					}


				}
			}
		}
	}
</style>